package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicUpdLessonController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/lesson/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String cover = (StringUtils.isNotBlank(reqModel.cover) ? reqModel.cover : "https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/136dc44072f22ac951a1e913f91488df.jpg");
        String name = StringTrimUtil.trim(reqModel.name);
        String summary = reqModel.summary;
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String catalogId3 = reqModel.catalogId3;

        YxyLessonsTopicLesson lesson = lessonRepository.findById(lessonId);
        if (null != lesson) {
            lesson.setCover(cover);
            lesson.setName(name);
            lesson.setSync(0);
            lesson.setSummary(summary);
            lesson.setCatalogId1(catalogId1);
            lesson.setCatalogId2(catalogId2);
            lesson.setCatalogId3(catalogId3);
            lesson.setIsVisible(0);
            lessonRepository.save(lesson);
        }
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public String cover;
        public String name;
        public String summary;
        public String catalogId1;
        public String catalogId2;
        public String catalogId3;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "未获取到课程");
            Assert.isTrue(StringUtils.isNotBlank(name), "课程名称不能为空");
            Assert.isTrue(StringUtils.isNotBlank(catalogId1), "一级栏目不能为空");
            Assert.isTrue(StringUtils.isNotBlank(catalogId2), "二级栏目不能为空");
            Assert.isTrue(StringUtils.isNotBlank(catalogId3), "三级栏目不能为空");
            Assert.isTrue(name.length() > 0 && name.length() < 36, "请输入1-35个字的视频名称");
            Assert.isTrue(StringUtils.isNotBlank(summary), "课程简介不能为空");
            Assert.isTrue(summary.length() > 0 && summary.length() < 501, "请输入1-500个字的课时简介");
        }
    }

    public static class RespModel extends YxyApiRespModel {

    }
}
